/**
 * @name         RepNET Credit Element Model
 * @version      1.0a
 * @author       Kasun Rathnayaka <kazar55@gmail.com>
 * @author       Ramindu Deshapriya <rasade88@gmail.com>
 * @author 		 Prabodha Dissanayake <prabodha89@gmail.com>
 * @author 		 Thusira Dissanayake <thusira14@gmail.com>
 * @about        Developed by MiDi
 * @license	 http://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License (LGPL)
 * @lastModified 2012.09.24
 */
package com.midi.repnet.models;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;

import com.midi.repnet.main.Main;

public class CreditElement {
		static String sql_agency;
		static String sql_shop;
		static ResultSet rs_agency;
		static Statement statement_agency;
		static Statement statement_shop;
		static Statement statement_region;
		static ResultSet rs_shop;
		static ResultSet rs_region;
		
		String bill_no;
		float amount;
		String date;
		String agency;
		String shop;
		String region;
		float remainder;
		String dates;
		
		public CreditElement(String bill_no,String agency_id, String shop_id, String date, float amount) {
			
			try {
				statement_agency= Main.dbConnection.createStatement();
				statement_shop= Main.dbConnection.createStatement();
				statement_region = Main.dbConnection.createStatement();
				
				sql_agency="SELECT * From agency WHERE id='"+agency_id+"'";  // Select all the agency that have Credit overdue
				sql_shop="SELECT * From shops WHERE id='"+shop_id+"'"; 
				
				rs_agency= statement_agency.executeQuery(sql_agency);
				rs_shop=statement_shop.executeQuery(sql_shop);
				rs_region = statement_region.executeQuery("SELECT r.name FROM regions as r LEFT JOIN shops as s ON s.region_id=r.id WHERE s.id='"+shop_id+"'");
				while (rs_agency.next()) {
				    this.agency = rs_agency.getString("name");
				}
				while (rs_shop.next()) {
				    this.shop = rs_shop.getString("name");
				}
				while (rs_region.next()) {
					this.region = rs_region.getString("name");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			this.bill_no=bill_no;
			this.date=date;
			this.amount=amount;			
			
		}
		public CreditElement(String bill_no,String agency_id, String shop_id, String date, float amount, float remainder) {
			
			try {
				statement_agency= Main.dbConnection.createStatement();
				statement_shop= Main.dbConnection.createStatement();
				statement_region = Main.dbConnection.createStatement();
				
				sql_agency="SELECT * From agency WHERE id='"+agency_id+"'";  // Select all the agency that have Credit overdue
				sql_shop="SELECT * From shops WHERE id='"+shop_id+"'"; 
				
				rs_agency= statement_agency.executeQuery(sql_agency);
				rs_shop=statement_shop.executeQuery(sql_shop);
				rs_region = statement_region.executeQuery("SELECT r.name FROM regions as r LEFT JOIN shops as s ON s.region_id=r.id WHERE s.id='"+shop_id+"'");
				while (rs_agency.next()) {
				    this.agency = rs_agency.getString("name");
				}
				while (rs_shop.next()) {
				    this.shop = rs_shop.getString("name");
				}
				while (rs_region.next()) {
					this.region = rs_region.getString("name");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			this.bill_no = bill_no;
			this.date = date;
			this.amount = amount;	
			this.remainder = remainder;
			
		}

		public String getBill_no() {
			return bill_no;
		}

		public void setBill_no(String bill_no) {
			this.bill_no = bill_no;
		}

		public String getAmount() {
			DecimalFormat df = new DecimalFormat();
	        df.setMinimumFractionDigits(2);
	        df.setMaximumFractionDigits(2);
	        df.setGroupingUsed(true);
			return df.format(amount);
		}

		public void setAmount(float amount) {
			this.amount = amount;
		}

		public String getDate() {
			return date;
		}

		public void setDate(String date) {
			this.date = date;
		}

		public String getAgency() {
			return agency;
		}

		public void setAgency(String agency) {
			this.agency = agency;
		}

		public String getShop() {
			return shop;
		}

		public void setShop(String shop) {
			this.shop = shop;
		}
		public String getRegion() {
			return this.region;
		}
		public String getRemainder(){
			DecimalFormat df = new DecimalFormat();
	        df.setMinimumFractionDigits(2);
	        df.setMaximumFractionDigits(2);
	        df.setGroupingUsed(true);
			return df.format(this.remainder);
		}

		

		
}
